home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Programming / C / hf^k-1.dms / in.adf / Examples.Lha / DebugTutorial / address.c next >
Encoding:
C/C++ Source or Header  |  1996-02-13  |  2.7 KB  |  127 lines

  1.  
  2. #include "address.h"
  3. #include <assert.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <pragma/exec_lib.h>
  7. #include <clib/alib_protos.h>
  8.  
  9. // kopiere einen String in den dynamischen Speicher
  10. static char *copystr(char *s)
  11. {
  12.     char *r = NULL;
  13.     if (s != NULL)
  14.     {
  15.         int l = strlen(s)+1;
  16.         r = malloc(l);
  17.         if (r != NULL)
  18.         {
  19.             memcpy(r,s,l);
  20.         };
  21.     };
  22.     return r;
  23. }
  24.  
  25. // gebe einen String auf dem dynamischen Speicher frei
  26. static void freestr(char *s)
  27. {
  28.     if (s != NULL)
  29.         free(s);
  30. }
  31.  
  32. // formatierte Ausgabe auf einer Datei für Addressausgaben
  33. static void printstr(FILE *f, char *header, char *s)
  34. {
  35.     assert(f != NULL && header != NULL);
  36.     if (s != NULL)
  37.         fprintf(f,"%s%s\n",header,s)
  38.     else
  39.         fprintf(f,"%s <unbekannt>\n",header);
  40. };
  41.  
  42. // alloc an address and copy parameters.
  43. struct Address *allocAddress(char *name, char *firstname, char *street, char *town)
  44. {
  45.     struct Address *adr = malloc(sizeof(struct Address));
  46.     if (adr)
  47.     {
  48.         adr->name = copystr(name);
  49.         adr->firstname = copystr(firstname);
  50.         adr->street = copystr(street);
  51.         adr->town = copystr(town);
  52.     };
  53.     return adr;
  54. }
  55.  
  56. // free an address
  57. void freeAddress(struct Address *adr)
  58. {
  59.     if (adr != NULL)
  60.     {
  61.         freestr(adr->name);
  62.         freestr(adr->firstname);
  63.         freestr(adr->street);
  64.         freestr(adr->town);
  65.     };
  66. }
  67.  
  68. static struct MinList addressheader;
  69. static struct MinList *addresslist = NULL;
  70.  
  71. // add an address to the addresslist
  72. void addAddress(struct Address *adr)
  73. {
  74.     assert(adr != NULL);
  75.     if (addresslist == NULL)
  76.     {
  77.         NewList((struct List *) &addressheader);
  78.         addresslist = &addressheader;
  79.     };
  80.     AddTail((struct List *) addresslist,(struct Node *) adr);
  81. }
  82.  
  83. // alloc an address and read from ASCII mask
  84. struct Address *readAddressmask()
  85. {
  86.     static char name[80], firstname[80], street[80], town[80];
  87.     printf("\fAdresseingabe:\n");
  88.     printf("Nachname: "); fflush(stdout);
  89.     gets(name);
  90.     printf("Vorname : "); fflush(stdout);
  91.     gets(firstname);
  92.     printf("Straße  : "); fflush(stdout);
  93.     gets(street);
  94.     printf("Wohnort : "); fflush(stdout);
  95.     gets(town);
  96.     return allocAddress(name,firstname,street,town);
  97. }
  98.  
  99. // write an address in an ASCII mask
  100. void writeAddressmask(FILE *f, struct Address *adr)
  101. {
  102.     assert(f != NULL && adr != NULL);
  103.     printstr(f,"Name   : ",adr->name);
  104.     printstr(f,"Vorname: ",adr->firstname);
  105.     printstr(f,"Straße : ",adr->street);
  106.     printstr(f,"Wohnort: ",adr->town);
  107. }
  108.  
  109. // write addresslist to an ASCII mask
  110. void writeAddresslist(FILE *f)
  111. {
  112.     assert(f != NULL);
  113.     if (addresslist == NULL || IsListEmpty((struct List *) addresslist))
  114.         printf("Keine Adressen in der Liste.\n")
  115.     else
  116.     {
  117.         struct Address *adr = (struct Address *) addresslist->mlh_Head;
  118.         printf("\f");
  119.         while (adr->node.mln_Succ != NULL)
  120.         {
  121.             writeAddressmask(f,adr);
  122.             fprintf(f,"\n");
  123.             adr = (struct Address *) adr->node.mln_Succ;
  124.         };
  125.     };
  126. }
  127.